package com.easy.work.app;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.easy.work.R;
import com.easy.work.component.SysWaitingDialog;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by winning_zzg on 2017/4/6.
 */

public class BaseActivity extends FragmentActivity {
    public String tag;
    protected BaseActivity oThis;
    public static boolean refreshView = false;
    private SysWaitingDialog dialog;
    public List<BaseFragment> notifyBackPressList = new ArrayList<BaseFragment>();
    protected boolean isShowTop = false;
    /**
     * 是否设置电池栏
     */
    protected boolean isSetBatteryBar = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        initBatteryBar();
        oThis = this;
        tag = getClass().getSimpleName();
    }

    private void initBatteryBar() {
        if (!isSetBatteryBar) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !isShowTop) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    /**
     * 显示进度对话框
     */
    public void showMyDialog(String stemp) {
        if (isFinishing()) {
            return;
        }
//        if (!isActivityTop(this)) {
//            return;
//        }
        dialog = new SysWaitingDialog(this, stemp, R.style.CommProgressDialog, R.drawable.frame);
        dialog.show();
        mBaseHandler.sendEmptyMessageDelayed(1, 15 * 1000);

    }

    /**
     * 显示进度对话框
     */
    public void showDialog() {
        showMyDialog("加载中！");
    }


    public Handler mBaseHandler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            closeDialog();
        }
    };


    /**
     * 关闭等待层
     *
     * @throws
     * @since 1.0.0
     */
    public void closeDialog() {
        if (isFinishing()) {
            return;
        }
//        if (!isActivityTop(this)) {
//            return;
//        }
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        closeDialog();
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }


    public void initTitle(String title) {

        TextView titleTv = (TextView) findViewById(R.id.title_txt);
        titleTv.setText(title);
        RelativeLayout backLayout = (RelativeLayout) findViewById(R.id.title_left_back);
        TextView leftTv = (TextView) findViewById(R.id.title_left_txt);
        leftTv.setBackgroundResource(R.mipmap.fanhui_left);
        backLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                finish();

            }
        });


    }

    public void initTitle(String title, String rightTxt, View.OnClickListener rightListener) {

        TextView titleTv = (TextView) findViewById(R.id.title_txt);
        titleTv.setText(title);
        RelativeLayout backLayout = (RelativeLayout) findViewById(R.id.title_left_back);
        TextView leftTv = (TextView) findViewById(R.id.title_left_txt);
        leftTv.setBackgroundResource(R.mipmap.fanhui_left);
        backLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                finish();

            }
        });

        TextView tvRight = (TextView) findViewById(R.id.title_right_txt);
        tvRight.setText(rightTxt);
        tvRight.setOnClickListener(rightListener);

    }

    /**
     * 替换公用layout中 layout_set_frag的frag
     *
     * @param fragment
     */
    protected void replaceFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
    }

    /**
     * 替换公用layout中 layout_set_frag的frag
     *
     * @param fragment
     */
    protected void replaceFragment(Fragment fragment, Bundle bundle) {
        fragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
    }

    /**
     * 替换fragment
     *
     * @param fragment
     */
    public void replaceFragment(Fragment fragment, int fragmentId, Bundle bundle) {
        fragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(fragmentId, fragment).commit();
    }

    /**
     * config current activity
     * fitsSystemWindows to  Override return false will not fitsSystemWindows
     * must used with xml contView
     *
     * @return
     */
    public boolean isActivityFitsSystemWindows() {
        return true;
    }


//    /**
//     * 设置电池栏颜色
//     */
//    public void configSystemBar() {
//        try {
//            if (!isActivityFitsSystemWindows())
//                return;
//
//            //fitsSystemWindows是否修改状态栏的总开关   使用style 达不到效果，只能在xml布局中使用fitsSystemWindows属性
//            boolean flag = getResources().getBoolean(R.bool.fitsSystemWindows);
//            if (!flag)
//                return;
//
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                setTranslucentStatus(true);
//            }
//
//            statusBarTintResource();
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } catch (Error e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * for systemBar
     *
     * @param on
     */
    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    int common_title = R.color.white;

//    /**
//     * 状态栏颜色 或者图片
//     *
//     * @return
//     */
//    public int getStatusBarTintResource() {
//        return common_title;
//    }
//
//    public void setStatusBarTintResource(int id) {
//        common_title = id;
//    }
//
//    /**
//     * Override this method to setStatusBarTintResource
//     */
//    public void statusBarTintResource() {
//        SystemBarTintManager tintManager = new SystemBarTintManager(this);
//        tintManager.setStatusBarTintEnabled(true);
//        tintManager.setStatusBarTintResource(getStatusBarTintResource());
//    }

//    /**
//     * 判断某activity是否处于栈顶
//     *
//     * @return true在栈顶 false不在栈顶
//     */
//    private boolean isActivityTop(Activity context) {
//        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//        String name = manager.getRunningTasks(1).get(0).topActivity.getClassName();
//        String actName = context.getClass().getName();
//        return name.equals(actName);
//    }

}
